package com.ksd.modules.rbac.roles.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.enums.StatusEnums;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.convert.CopyConvertUtils;
import com.ksd.modules.rbac.api.entity.SysApis;
import com.ksd.modules.rbac.api.mapper.SysRoleApisMapper;
import com.ksd.modules.rbac.menus.entity.SysMenus;
import com.ksd.modules.rbac.menus.mapper.SysRoleMenusMapper;
import com.ksd.modules.rbac.roles.entity.SysRoles;
import com.ksd.modules.rbac.roles.mapper.SysRolesMapper;
import com.ksd.modules.rbac.roles.po.SysRolesSavePO;
import com.ksd.modules.rbac.roles.po.SysRolesSearchPO;
import com.ksd.modules.rbac.roles.service.ISysRolesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author Auto Generator
 * @since 2023-08-13
 */
@Service
public class SysRolesService extends BaseServiceImpl<SysRolesMapper, SysRoles> implements ISysRolesService {

    @Resource
    private SysRoleApisMapper sysRoleApisMapper;
    @Resource
    private SysRoleMenusMapper sysRoleMenusMapper;

    @Override
    public Map<String, Object> findMenusPermissionByRoleId(Long roleId) {
        Map<String, Object> map = new HashMap<>();
        List<SysApis> roleApis = new ArrayList<>();sysRoleApisMapper.findRoleApis(roleId);
        List<SysMenus> roleMenus = tree(sysRoleMenusMapper.findRoleMenus(roleId));
        map.put("permissions",roleApis);
        map.put("roleMenus",roleMenus);
        return map;
    }

    private List<SysMenus> tree(List<SysMenus> list){
        List<SysMenus> resultList = new ArrayList<>();
        for (SysMenus result : list) {
            if (result.getParentId() == 0L) {
                //调用方法给子类添加数据
                resultList.add(getMenuTree(result, list));
            }
        }
        return resultList;
    }

    private SysMenus getMenuTree(SysMenus result, List<SysMenus> list) {
        for (SysMenus menu : list) {
            //如果父类主键等于传过来实体类的ID
            if (menu.getParentId() == result.getId()) {
                if (result.getChildren() == null) {
                    result.setChildren(new ArrayList<>());
                }
                // 递归调用
                result.getChildren().add(getMenuTree(menu, list));
            }
        }
        return result;
    }

    /**
     * 查询角色接口
     * @return
     */
    @Override
    public List<SysRoles> findSysRoles(){
        LambdaQueryWrapper<SysRoles> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysRoles::getStatus, StatusEnums.STATUS.getCode());
        lambdaQueryWrapper.orderByAsc(SysRoles::getId);
        return this.list(lambdaQueryWrapper);
    }

    @Override
    public Page<SysRoles> findSysRolesPage(SysRolesSearchPO sysRolesSearchPO) {
        LambdaQueryWrapper<SysRoles> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(sysRolesSearchPO.getStatus()), SysRoles::getStatus, sysRolesSearchPO.getStatus());
        if (StringUtils.isNotEmpty(sysRolesSearchPO.getKeyword())) {
            lambdaQueryWrapper.and(c -> c.like(SysRoles::getRoleName, sysRolesSearchPO.getKeyword())
                    .or().like(SysRoles::getRoleCode, sysRolesSearchPO.getKeyword()));
        }
        lambdaQueryWrapper.orderByDesc(SysRoles::getCreateTime);
        // 查询返回
        Page<SysRoles> page = new Page<>(sysRolesSearchPO.getPageNo(), sysRolesSearchPO.getPageSize());
        return this.page(page, lambdaQueryWrapper);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveUpdateSysRoles(SysRolesSavePO sysRolesSavePO) {
        SysRoles mayaSysRoles = CopyConvertUtils.copyProperties(sysRolesSavePO, SysRoles.class);
        return this.saveOrUpdate(mayaSysRoles);
    }

    @Override
    public boolean deleteSysRolesById(Long id) {
        return this.removeById(id);
    }

    @Override
    public SysRoles getSysRolesById(Long id) {
        return this.getById(id);
    }
}
